package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.CourseSchedulingDetail;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.Student;
import com.xmy.cultivate.feign.erp_admin.IAdminFeignClient;
import com.xmy.cultivate.service.AuthorityServic;
import com.xmy.cultivate.service.impl.AuthorityImpl;
import com.xmy.cultivate.mapper.StaffMapper;
import com.xmy.cultivate.mapper.StudentMapper;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/student")
public class StudentController extends BaseController{
    @Autowired
    StudentMapper studentMapper;

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    AuthorityServic authorityServic;

    @Autowired
    IAdminFeignClient iAdminFeignClient;

    @GetMapping("/getStudentMyList")
    @ApiOperation("获取我的学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,value = "学生姓名" , required = false, dataType = "String"),
            @ApiImplicitParam(name = "tel" ,value = "学生电话" , required = false ,dataType = "String"),
            @ApiImplicitParam(name = "schoolId",value = "校区id", required = true,dataType = "String"),
            @ApiImplicitParam(name = "pageNum",value = "分页数目" ,required = false ,dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "分页尺寸",required = false,dataType = "String")
    })
    public Result getStudentMyList(@RequestParam Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 100: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<Student> page = new Page<>(pageNum,pageSize);
        String adminId = String.valueOf(this.adminId);
        Integer laber = authorityServic.get(adminId);
        String name = (String) reMap.get("name");
        String tel = (String) reMap.get("tel");
        String schoolId =(String) reMap.get("schoolId");
        QueryWrapper queryWrapper = new QueryWrapper<>();
        //如果是校长则可以查询管辖下所有校区
        //如果是主管和财务则可以查找所选的全部校区
        if(laber == 3){
            return new Result(Code.OK,null,"此老师无学生信息");
        }
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.like(StringUtils.isNotBlank(name), "student_name", name);
        queryWrapper.like(StringUtils.isNotBlank(tel),"linkman_phone",tel);
        queryWrapper.eq("s.deleted",0);
        queryWrapper.eq("g.teacher_id",adminId);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"sg.school_id",schoolId);
        queryWrapper.eq("reading_status",1);
        queryWrapper.eq("sg.status",1);
        queryWrapper.eq("sg.year_part",2023);
        queryWrapper.eq("sg.quarter_num",2);
        queryWrapper.groupBy("s.id");
        studentMapper.selectOther(page,queryWrapper);
        return new Result(Code.OK,new PageResult(page.getRecords(), page.getTotal(), page.getPages()),"获取学生信息成功");
    }

    @GetMapping("/getStudentOtherList")
    @ApiOperation("获取其他学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,value = "学生姓名" , required = false, dataType = "String"),
            @ApiImplicitParam(name = "tel" ,value = "学生电话" , required = false ,dataType = "String"),
            @ApiImplicitParam(name = "schoolId",value = "校区id", required = true,dataType = "String"),
            @ApiImplicitParam(name = "pageNum",value = "分页数目" ,required = false ,dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "分页尺寸",required = false,dataType = "String")
    })
    public Result getStudentOtherList(@RequestParam Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 100: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<Student> page = new Page<>(pageNum,pageSize);
        String adminId = String.valueOf(this.adminId);
        Integer laber = authorityServic.get(adminId);
        String name = (String) reMap.get("name");
        String tel = (String) reMap.get("tel");
        String schoolId = (String) reMap.get("schoolId");
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(laber == 1){
            return new Result(Code.OK,null,"无权限");
        }
        //如果是校长则可以查询管辖下所有校区
        if(laber == 2){
            Staff staff = staffMapper.selectByAdminId(Long.valueOf(adminId));
            if(staff.getSchoolId().equals(schoolId) || String.valueOf(schoolId) == ""){
                queryWrapper.eq("s.school_id",staff.getSchoolId());
                queryWrapper.ne("g.teacher_id",adminId);
            }else{
                return new Result(Code.ERROR,null,"非本校区");
            }
        }
        //如果是主管和财务则可以查找所选的全部校区
        queryWrapper.like(StringUtils.isNotBlank(name), "student_name", name);
        queryWrapper.like(StringUtils.isNotBlank(tel),"linkman_phone",tel);
        if(String.valueOf(schoolId) != ""){
            queryWrapper.eq("sg.school_id",schoolId);
        }
        queryWrapper.ne("g.teacher_id",adminId);
        queryWrapper.eq("reading_status",1);
        queryWrapper.eq("sg.status",1);
        queryWrapper.eq("sg.year_part",2023);
        queryWrapper.eq("sg.quarter_num",2);
        queryWrapper.groupBy("s.id");
        studentMapper.selectOther(page,queryWrapper);
        return new Result(Code.OK,new PageResult(page.getRecords(), page.getTotal(), page.getPages()),"获取学生信息成功");
    }

    @GetMapping("/signUpStu")
    public Result signUpStu(@RequestParam Map reMap,
                            @RequestParam(value = "excludeStudentId",required = false) Long excludeStudentId){
        Result rs;
        try {
            rs = iAdminFeignClient.studentSignUpStu(reMap,excludeStudentId);
        }catch (Exception e){
            rs = new Result(Code.ERROR,"", "系统更新中，请1分钟后再使用");
        }
        return rs;
    }

    @ApiOperation("查询学生的亲属")
    @GetMapping("/kinsfolk")
    public Result getKinsfolk(@RequestParam Map reMap) {
        Result rs;
        try {
            rs = iAdminFeignClient.studentGetKinsfolk(reMap);
        }catch (Exception e){
            rs = new Result(Code.ERROR,"", "系统更新中，请1分钟后再使用");
        }
        return rs;
    }

    @ApiOperation("获取学生和课程")
    @GetMapping("/getStudentIdAndCourseId")
    public Result getStudentIdAndCourseId(@RequestParam Map reMap) {
        Result rs;
        try {
            rs = iAdminFeignClient.studentGetStudentIdAndCourseId(reMap);
        }catch (Exception e){
            rs = new Result(Code.ERROR,"", "系统更新中，请1分钟后再使用");
        }
        return rs;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") Long id){
        Result rs;
        try {
            rs = iAdminFeignClient.findById(id);
        }catch (Exception e){
            rs = new Result(Code.ERROR,"", "系统更新中，请1分钟后再使用");
        }
        return rs;
    }

}
